package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbSeckillOrderExample;
import com.pyg.pojo.TbSeckillOrderExample.Criteria;
import com.pyg.mapper.TbSeckillOrderMapper;
import com.pyg.pojo.TbSeckillOrder;
import com.pyg.goods.service.TbSeckillOrderService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbSeckillOrderServiceImpl implements TbSeckillOrderService {
	@Autowired
	private TbSeckillOrderMapper tbSeckillOrderMapper;

	@Override
	public List<TbSeckillOrder> findAll() {
		return tbSeckillOrderMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbSeckillOrder> page = (Page<TbSeckillOrder>) tbSeckillOrderMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbSeckillOrder
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbSeckillOrder tbSeckillOrder, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbSeckillOrderExample example = null;
		if (tbSeckillOrder != null) {
			example = new TbSeckillOrderExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getSeckillId())) {
				criteria.andSeckillIdEqualTo(tbSeckillOrder.getSeckillId() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getMoney())) {
				criteria.andMoneyEqualTo(tbSeckillOrder.getMoney() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getUserId())) {
				criteria.andUserIdLike("%" + tbSeckillOrder.getUserId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getSellerId())) {
				criteria.andSellerIdLike("%" + tbSeckillOrder.getSellerId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getCreateTime())) {
				criteria.andCreateTimeEqualTo(tbSeckillOrder.getCreateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getPayTime())) {
				criteria.andPayTimeEqualTo(tbSeckillOrder.getPayTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getStatus())) {
				criteria.andStatusLike("%" + tbSeckillOrder.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getReceiverAddress())) {
				criteria.andReceiverAddressLike("%" + tbSeckillOrder.getReceiverAddress() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getReceiverMobile())) {
				criteria.andReceiverMobileLike("%" + tbSeckillOrder.getReceiverMobile() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getReceiver())) {
				criteria.andReceiverLike("%" + tbSeckillOrder.getReceiver() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillOrder.getTransactionId())) {
				criteria.andTransactionIdLike("%" + tbSeckillOrder.getTransactionId() + "%");
			}
		}

		Page<TbSeckillOrder> page = (Page<TbSeckillOrder>) tbSeckillOrderMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbSeckillOrder tbSeckillOrder) {
		tbSeckillOrderMapper.insert(tbSeckillOrder);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbSeckillOrderMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbSeckillOrder findOne(Long id) {
		return tbSeckillOrderMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbSeckillOrder tbSeckillOrder) {
		tbSeckillOrderMapper.updateByPrimaryKey(tbSeckillOrder);
	}

}
